package test;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class TESTAction {
	public void ShowPages() throws IOException {
		//获取strust2内存中已经加载的properties属性文件
		ActionSupport actionSupport =new ActionSupport();
		String baseUrl = actionSupport.getText("hostflie.baseUrl");
		String PageManagerName = actionSupport.getText("hostflie.PageManager");
		String allPath = baseUrl +"/"+ PageManagerName;
		ArrayList arrayList = circleGet(allPath);

		JSONArray jsonArray = JSONArray.fromObject(arrayList);
		System.out.println(jsonArray.toString());
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter printWriter = response.getWriter();
		printWriter.print(arrayList);
	}
	public ArrayList circleGet(String path) {
		String allPath = path;
	    File file=new File(allPath);
	    File[] tempList = file.listFiles();
	    ArrayList al = new ArrayList();
	    System.out.println("该目录下对象个数："+tempList.length);
		for (int i = 0; i < tempList.length; i++) {
	    	TrueData trueData = new TrueData();
	    	 trueData.setId(i);
		     trueData.setState("closed");
		     trueData.setText(tempList[i].getName());
	    	 if (tempList[i].isDirectory()) {
	    		 trueData.setIconCls("icon-folder");
	    		 ArrayList arrayList = circleGet(tempList[i].getPath());
			      trueData.setChildren(arrayList);
			 }
		     if (tempList[i].isFile()) {
		    	 trueData.setIconCls("icon-File");
		     }
		     al.add(trueData);
	    }
		JSONArray jsonArray = JSONArray.fromObject(al);
		System.out.println(jsonArray);
		return al;
	}
}

class TrueData {
	private int id;
	private String iconCls;
	private String text;
	private String state;
	//private Boolean checked;
	private String attributes;
	private ArrayList children;

	public String getIconCls() {
		return iconCls;
	}
	public void setIconCls(String iconCls) {
		this.iconCls = iconCls;
	}
	public ArrayList getChildren() {
		return children;
	}
	public void setChildren(ArrayList children) {
		this.children = children;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
//	public Boolean getChecked() {
//		return checked;
//	}
//	public void setChecked(Boolean checked) {
//		this.checked = checked;
//	}
	public String getAttributes() {
		return attributes;
	}
	public void setAttributes(String attributes) {
		this.attributes = attributes;
	}
}
   